package main

import (
	"context"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var db *gorm.DB

const (
	UnknowLogin = iota
	IsLogin
	NotLogin
)

const (
	OffLine = iota
	OnLine
)

const (
	TaskUnStart = iota
	TaskRunning
	TaskFailed
	TaskSuccess
	TaskCancel
)

func initDB() {
	var err error
	//db, err = gorm.Open("sqlite3", "test.db")
	db, err = gorm.Open("mysql", "root:password@/douyin?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		panic(err)
	}
	db.AutoMigrate(&Device{}, &Pack{}, &Task{}, &Video{}, &Job{})

}

type Device struct {
	ID     string `gorm:"primary_key"`
	Name   string
	Status int

	Packs         []Pack
	ch            chan Task
	cancelCh      chan int
	CurrentTask   Task
	currentCancel context.CancelFunc
}

type Pack struct {
	ID          int
	DeviceID    string `gorm:"unique_index:did_name"`
	Name        string `gorm:"unique_index:did_name"`
	LoginStatus int
	LoginName   string
	Follow      string
	Fans        string
	AuthorId    string
}

type Task struct {
	ID       int
	DeviceID string
	JobID    int
	PName    string
	Type     int
	Extra    string
	Status   int
	Error    string
	Trys     int
}

type Video struct {
	ID       int
	AuthorID string `gorm:"unique_index:v_t"`
	Title    string `gorm:"unique_index:v_t"`
	Origin   string
	Target   string
	Status   int
	PID      int
	IsUse    int
}

type Job struct {
	ID      int
	Type    int
	Devices string

	Tasks []Task
}
